package com.ruoyi.mall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.mall.product.domain.entity.PmsAttr;
import com.ruoyi.mall.product.domain.entity.PmsSkuAttrValue;
import com.ruoyi.mall.product.domain.repository.PmsAttrMapper;
import com.ruoyi.mall.product.domain.repository.PmsSkuAttrValueMapper;
import com.ruoyi.mall.product.domain.service.IPmsAttrService;
import com.ruoyi.mall.product.domain.service.IPmsSkuAttrValueService;
import com.ruoyi.mall.product.domain.vo.PmsAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: zqs-mall
 * @description:
 * @author: Mr.Zhang
 * @create: 2022-06-03 22:45
 **/
@Service
public class PmsAttrAppService {
    @Autowired
    private IPmsAttrService pmsAttrService;
    @Autowired
    private PmsAttrMapper pmsAttrMapper;

    @Autowired
    private PmsSkuAttrValueMapper pmsSkuAttrValueMapper;

    @Autowired
    private IPmsSkuAttrValueService pmsSkuAttrValueService;

    /**
     * 查询商品属性
     *
     * @param id 商品属性主键
     * @return 商品属性
     */
    public PmsAttr selectPmsAttrById(Long id) {
        return pmsAttrService.selectPmsAttrById(id);
    }

    /**
     * 查询商品属性列表
     *
     * @param pmsAttr  商品属性
     * @param attrType
     * @return 商品属性
     */
    public List<PmsAttr> selectPmsAttrList(PmsAttr pmsAttr, String attrType) {
        return pmsAttrService.selectPmsAttrList(pmsAttr, attrType);
    }

    /**
     * 新增商品属性
     *
     * @param pmsAttr 商品属性
     * @return 结果
     */
    public int insertPmsAttr(PmsAttrVo pmsAttr) {
        return pmsAttrService.insertPmsAttr(pmsAttr);
    }

    /**
     * 修改商品属性
     *
     * @param pmsAttr 商品属性
     * @return 结果
     */
    public int updatePmsAttr(PmsAttrVo pmsAttr) {
        return pmsAttrService.updatePmsAttr(pmsAttr);
    }

    /**
     * 批量删除商品属性
     *
     * @param ids 需要删除的商品属性主键
     * @return 结果
     */
    public int deletePmsAttrByIds(Long[] ids) {
        return pmsAttrService.deletePmsAttrByIds(ids);
    }

    /**
     * 删除商品属性信息
     *
     * @param id 商品属性主键
     * @return 结果
     */
    public int deletePmsAttrById(Long id) {
        return pmsAttrService.deletePmsAttrById(id);
    }

    /**
     * 修改商品属性状态
     *
     * @param pmsAttr
     * @return
     */
    public int updateStatusPmsAttr(PmsAttr pmsAttr) {
        return pmsAttrService.updateStatusPmsAttr(pmsAttr);
    }

    public List<PmsAttr> selectList(PmsAttr pmsAttr) {
        return pmsAttrMapper.selectList(null);
    }

    /**
     * 查询spu信息
     *
     * @param spuId
     * @return
     */
    public List<PmsSkuAttrValue> baseAttrListForSpu(Long spuId) {
        return pmsSkuAttrValueMapper.selectList(new QueryWrapper<PmsSkuAttrValue>().eq("spu_id", spuId));

    }

    /**
     * 更新spu信息
     *
     * @param spuId
     * @param list
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updatePmsAttrValue(Long spuId, List<PmsSkuAttrValue> list) {
        //1.删除这个spuId之前对应的所有属性
        pmsSkuAttrValueMapper.delete(new QueryWrapper<PmsSkuAttrValue>().eq("spu_id", spuId));

        List<PmsSkuAttrValue> collect = list.stream().map(item -> {
            item.setId(spuId);
            return item;
        }).collect(Collectors.toList());

        boolean b = pmsSkuAttrValueService.saveBatch(collect);

        return b ? 1 : 0;
    }
}
